package map.tools.interactions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.eclipse.emf.common.util.EList;

import uimigration.Interactions.Action;
import uimigration.Plateform.Device;
import uimigration.WidgetsModel.APIModel;
import uimigration.WidgetsModel.Event;
import uimigration.WidgetsModel.Widget;
import uimigration.mappingmodel.MappingActionEvent;
import uimigration.mappingmodel.MappingTable;
import uimigration.mappingmodel.impl.MappingTableImpl;

public class DeviceActionTable implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<DeviceEvents> table=null;
	private APIModel apimodel;
	private String fileName;
	private List<Device> ld=null;

	public DeviceActionTable(MappingTable mapping,List<Device> ld){
		
		if(table==null)
			table= new ArrayList<DeviceEvents>();
		this.setLd(ld);
	}
	public DeviceActionTable(APIModel apimodel, MappingTable mapping,List<Device> ld){
		if(table==null)
			table= new ArrayList<DeviceEvents>();
		Vector<String> events = new Vector<String>();
		for(Widget w: apimodel.getContains()){
			for(Event event:w.getEvents())
				if(!events.contains(event.getName()))
						events.add(event.getName());
		}
		for(MappingActionEvent beh:mapping.getActionsMapping()){
			for(uimigration.mappingmodel.Event we:beh.getMappedEvents()){
				if(events.contains(we.getName())){
					EList<Action> lb=we.getMappedActions();
					for(Action ac: we.getMappedActions())
					{	if(ac.getType()!=null && ld!=null){
							DeviceEvents eb= new DeviceEvents(we.getName(), ld.get(0),lb, ac.getType().toString());
							table.add(eb);
							events.remove(we.getName());
						}
					}
				}
			}
		}
	}

	
	public void addElement(String event, Device d, EList<Action> eList, String ty){
		DeviceEvents de= new DeviceEvents(event, d, eList, ty);
		if(table==null)
			table= new ArrayList<DeviceEvents>();
		table.add(de);
	}
	public List<Action> getEventAction(String event){
		List<Action> la= null;
		DeviceEvents de= foundElement(event);
		if(de!=null)
			return de.actions;
		
		return la;
		
	}
	private DeviceEvents foundElement(String event){
		for(DeviceEvents de:table)
			if(de.event.compareTo(event)==0)
				return de;
		
		return null;
	}
	
	public DeviceActionTable(APIModel apimodel, MappingTable mapping, Device d){
		if(table==null)
			table= new ArrayList<DeviceEvents>();
		Vector<String> events = new Vector<String>();
		for(Widget w: apimodel.getContains()){
			for(Event event:w.getEvents())
				if(!events.contains(event.getName()))
						events.add(event.getName());

		}
		for(MappingActionEvent act:mapping.getActionsMapping()){
			for(uimigration.mappingmodel.Event we:act.getMappedEvents()){
				if(events.contains(we.getName())){
					EList<Action> lb=we.getMappedActions();
					for(Action ac: we.getMappedActions())
					{	if(ac.getType()!=null){
							DeviceEvents eb= new DeviceEvents(we.getName(),d, lb, ac.getType().toString());
							table.add(eb);
							events.remove(we.getName());
						}
					}
				}
			}
		}
	}
	public void addElement(Device d,String event, Action[] action,String type){
		DeviceEvents eb= new DeviceEvents(event,d, action, type);
		table.add(eb);
	}
	public List<Action> getActions(String event){
		List<Action> actions= null;
		DeviceEvents eb= foundEvent(event);
		if(eb!=null && eb.actions!=null)
			actions= new ArrayList<Action>(eb.actions);
		return actions;
	}
	public boolean removeElement (String eventName){
		//TODO: Remove Element
		return true;
	}
	public void addAction(String event, Action a){
		DeviceEvents e=foundEvent(event);
		if(e!=null)
			e.addAction(a);
	}
	public boolean isProperty(String name){
		if(!isEvent(name) )
			return true;
		return false;
	}
	private DeviceEvents foundEvent(String event){
		for(DeviceEvents eb:table){
			if(eb.event.compareTo(event)==0)
				return eb;
		}
		return null;
	}


	public void setTable(MappingTableImpl mapping, Device d){
		if(table==null)
			table= new ArrayList<DeviceEvents>();		
		if(mapping !=null)
			for(MappingActionEvent beh:mapping.getActionsMapping()){
				for(uimigration.mappingmodel.Event ev:(beh.getMappedEvents()))
					for(Action ac: ev.getMappedActions())
					{	if(ac.getType()!=null)
							addElement(ev.getName(),d,  ev.getMappedActions(), ac.getType().getName());
					}
			}
	}
	
	
	public boolean isEvent(String name){
		if(foundEvent(name)!=null)
			return true;
		else return false;
	}
	public boolean isLinkedProperty(String name){
		return false;
	}
	public List<String> getLinkedPropertie(String name){
		DeviceEvents eb= foundEvent(name);
		if(eb!=null)
		{
		}
		return null;
	}

	public void setApimodel(APIModel apimodel) {
		this.apimodel = apimodel;
	}

	public APIModel getApimodel() {
		return apimodel;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileName() {
		return fileName;
	}
	public Object[][]getData(){
		Object [][] data;
		data=new Object[table.size()][5];
		int i=0;
		int j=2;
		for(DeviceEvents eb:table){
			data[i][0]=eb.device;
			data[i][1]= eb.event;
			if(eb.type.equals("Activate"))
				j=2;
			else if(eb.type.equals("MoveDevice"))
				j=3;
			else if(eb.type.equals("EnterData"));
				j=4;
			data[i][j]=eb.actions;
			i++;
		}
		return data;
		
	}
	public void setLd(List<Device> ld) {
		this.ld = ld;
	}
	public List<Device> getLd() {
		return ld;
	}

}
class DeviceEvents{
	public Device device;
	public String event;
	public String type;
	public List<Action> actions;
	
	public DeviceEvents(String ev, Device d,  EList<Action> lb,String ty){
		event=ev;
		if(d!=null )
			device=d;
		type=ty;
		actions= new ArrayList<Action>();
		if (lb!=null)
		for(Action a:lb)
		{
			actions.add(a);
		}
	}
	public DeviceEvents(String ev, Device d, Action[] la, String ty) {
		event=ev;
		device=d;
		type=ty;
		actions= new ArrayList<Action>();
		if (la!=null)
		for(Action a:la)
		{
			actions.add(a);
		}

	}
	public List<String> linkedProperties() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void addAction(Action b){
		actions.add(b);
	}
}
